package com.sf.fy.uav.applet.scenicspot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sf.fy.uav.common.request.operate.*;
import com.sf.fy.uav.common.response.operational.BusinessRes;
import com.sf.fy.uav.common.response.operational.PlacementPointRes;
import com.sf.fy.uav.common.response.operational.ScenicSpotRes;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.PhoneEncoderUtil;
import com.sf.fy.uav.common.utils.RespJsonPageData;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.controller.BusinessRest;
import com.sf.fy.uav.controller.CommodityRest;
import com.sf.fy.uav.controller.OrderCommodityRest;
import com.sf.fy.uav.entity.Business;
import com.sf.fy.uav.entity.Commodity;
import com.sf.fy.uav.entity.OrderCommodity;
import com.sf.fy.uav.order.controller.OrderInfoRest;
import com.sf.fy.uav.order.entity.OrderInfo;
import com.sf.fy.uav.rest.PlacementPointRest;
import com.sf.fy.uav.rest.ScenicSpotRest;
import com.sf.fy.uav.withdrawal.entity.ScenicSpot;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author ta136817
 */
@RestController
@RequestMapping("/home/scenicSpot/")
@RequiredArgsConstructor
@Api(tags = "景区管理")
public class ScenicSpotSysController {

    private final ScenicSpotRest scenicSpotRest;

    private final PlacementPointRest placementPointRest;

    private final BusinessRest businessRest;

    private final PhoneEncoderUtil phoneEncoderUtil;

    private final OrderCommodityRest orderCommodityRest;

    private final CommodityRest commodityRest;

    private final OrderInfoRest orderInfoRest;
    @GetMapping("get-scenicSpot")
    public RespJsonPageData<ScenicSpotRes> getScenicSpot(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String name, @RequestParam(required = false) Integer status) {
        ScenicSpotReq scenicSpotReq = new ScenicSpotReq();
        scenicSpotReq.setPage(page);
        scenicSpotReq.setSize(size);
        scenicSpotReq.setName(name);
        scenicSpotReq.setStatus(status);
        return scenicSpotRest.getPageScenicSpot(scenicSpotReq);
    }
    @PostMapping ("add-scenicSpot")
    public Rs<Integer> addScenicSpot(@RequestBody ScenicSpotReq scenicSpotReq) {
       return scenicSpotRest.addPageScenicSpot(scenicSpotReq);
    }
    @PostMapping ("edit-scenicSpot")
    public Rs<Integer> editScenicSpot(@RequestBody ScenicSpotReq scenicSpotReq) {
        return scenicSpotRest.editScenicSpot(scenicSpotReq);
    }

    @GetMapping("get-placementPoint")
    public RespJsonPageData<PlacementPointRes> getPlacementPoint(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String name, @RequestParam(required = false) Integer status,@RequestParam String scenicSpotId) {
        PlacementPointReq placementPointReq = new PlacementPointReq();
        placementPointReq.setPage(page);
        placementPointReq.setSize(size);
        placementPointReq.setName(name);
        placementPointReq.setStatus(status);
        placementPointReq.setScenicSpotId(scenicSpotId);
        return placementPointRest.getPagePlacementPoint(placementPointReq);
    }

    @PostMapping ("add-placementPoint")
    public Rs<Integer> addPlacementPoint(@RequestBody PlacementPointReq placementPointReq) {
        return placementPointRest.addPlacementPoint(placementPointReq);
    }
    @PostMapping ("edit-placementPoint")
    public Rs<Integer> editPlacementPoint(@RequestBody PlacementPointReq placementPointReq) {
        return placementPointRest.editPlacementPoint(placementPointReq);
    }
    @PostMapping ("del-placementPoint")
    public Rs<Integer> delPlacementPoint(@RequestBody PlacementPointReq placementPointReq) {
        return placementPointRest.delPlacementPoint(placementPointReq);
    }
    @GetMapping("get-business")
    public RespJsonPageData<BusinessRes> getBusiness(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String name, @RequestParam(required = false) Integer status,@RequestParam String scenicSpotId) {
        BusinessReq businessReq = new BusinessReq();
        businessReq.setPage(page);
        businessReq.setSize(size);
        businessReq.setName(name);
        businessReq.setStatus(status);
        businessReq.setScenicSpotId(scenicSpotId);

        LambdaQueryWrapper<Business> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(name),Business::getName,name)
                .eq(!ObjectUtils.isEmpty(status), Business::getStatus,status)
                .eq(Business::getScenicSpotId, scenicSpotId)
                .orderByDesc(Business::getCreateTime);
        Page<Business> businessPageList = businessRest.getBusinessesPage(page, size, wrapper);
        Set<String> businessIdSet = businessPageList.getRecords().stream().map(Business::getId).collect(Collectors.toSet());
        List<Commodity> commodityList = commodityRest.selectProductByBusinessIds(businessIdSet);
        Set<String> commoditySet = commodityList.stream().map(Commodity::getId).collect(Collectors.toSet());
        List<OrderCommodity> orderCommodityList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(commoditySet)){
            orderCommodityList = orderCommodityRest.getOrderCommodityInCommodityIds(commoditySet);
        }
        List<OrderCommodity> orderCommodityNewList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(orderCommodityList)){
            Set<String> orderSnSet = orderCommodityList.stream().map(OrderCommodity::getOrderSn).collect(Collectors.toSet());
            List<OrderInfo> orderInfoList = orderInfoRest.getOrderInfoListInTcWaybillNos(orderSnSet);
            Map<String,OrderInfo> waybillNoAndOrderInfoMap = orderInfoList.stream().collect(Collectors.toMap(OrderInfo::getTcWaybillNo, each -> each, (value1, value2) -> value1));
            orderCommodityNewList = orderCommodityList.stream().filter(orderCommodity -> !ObjectUtils.isEmpty(waybillNoAndOrderInfoMap.get(orderCommodity.getOrderSn()))).collect(Collectors.toList());
        }
        businessReq.setOrderCommodityNewList(BeanUtil.copyProperties(orderCommodityNewList, OrderCommodityQuery.class));
        return businessRest.getBusiness(businessReq);
    }

    @PostMapping("del-business")
    public Rs<Integer> delBusiness(@RequestBody MerchantReq merchantReq) {
      return businessRest.delBusiness(merchantReq);
    }
    @GetMapping("phone-number-decryption")
    public ScenicSpot getScenicSpot(@RequestParam String id) {
        ScenicSpot scenicSpot = scenicSpotRest.selectByIdNoByStatus(id);
        scenicSpot.setPhone(phoneEncoderUtil.decryptString(scenicSpot.getPhone()));
        return scenicSpot;
    }
}
